package robotleMvc.ui.glass
{
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.display3D.analysis.AnalysisServer;
	import _Pan3D.load.LoadInfo;
	import _Pan3D.load.LoadManager;
	import _Pan3D.texture.TextureManager;
	
	import _me.Scene_data;
	
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.VertexBuffer3D;
	import flash.geom.Matrix3D;
	import flash.utils.clearInterval;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;

	public class GlassDisplay3DSprite extends Display3DSprite
	{
		private var _stateBuffer:VertexBuffer3D;

		public function GlassDisplay3DSprite(context:Context3D)
		{
			super(context);
		}

		override protected function setVa():void
		{
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setVertexBufferAt(2, _stateBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setTextureAt(1, _objData.texture);
			_context.setTextureAt(0, Scene_data.sceneLightText);
			_context.drawTriangles(_objData.indexBuffer, 0, -1);

		}
		
		private function get lightMatrix3D():Matrix3D
		{
			var lightMatrix3D:Matrix3D=new Matrix3D;
			lightMatrix3D.prepend(Scene_data.light3D.cameraMatrix);
			lightMatrix3D.prepend(posMatrix);
			return lightMatrix3D
		}
	

		override protected function setVc():void
		{
			this.updateMatrix();

			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 5, Vector.<Number>([1, 1, 1, 0.8])); //专门用来存树的通道的
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 12, lightMatrix3D, true);
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,2, Vector.<Number>([0, 0, 0.5, 1]));
			
		
		}

		override protected function resetVa():void
		{
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setVertexBufferAt(2, null);
			_context.setTextureAt(1, null);
		}

		override protected function uplodToGpu():void
		{
			var vv:Vector.<Number>=new Vector.<Number>;
			var uu:Vector.<Number>=new Vector.<Number>;
			var ss:Vector.<Number>=new Vector.<Number>; //要确定每组成几个数据;暂为3跟顶点一样
			var ii:Vector.<uint>=new Vector.<uint>;
			var inum:int=_objData.vertices.length / 3;
			var xMove:Number=0;
			var zMove:Number=0;
			var sizeData:int=500;
			var angle:int=0;
			for (var i:int; i < 1; i++)
			{
				vv=vv.concat(_objData.vertices);
				uu=uu.concat(_objData.uvs);
				xMove=Math.random() * sizeData - sizeData / 2;
				zMove=Math.random() * sizeData - sizeData / 2;
				for (var k:int=0; k < inum; k++)
				{
					ss.push(xMove, 0, zMove); // 倾斜
				}
				for (var j:int=0; j < _objData.indexs.length; j++)
				{
					ii.push(_objData.indexs[j] + inum * i);
				}
			}

			pushBuffData(vv, ss, uu, ii);

		}

		private function pushBuffData(vv:Vector.<Number>, ss:Vector.<Number>, uu:Vector.<Number>, ii:Vector.<uint>):void
		{
			_objData.vertexBuffer=this._context.createVertexBuffer(vv.length / 3, 3);
			_objData.vertexBuffer.uploadFromVector(Vector.<Number>(vv), 0, vv.length / 3);

			_stateBuffer=this._context.createVertexBuffer(ss.length / 3, 3); //存状态;
			_stateBuffer.uploadFromVector(Vector.<Number>(ss), 0, ss.length / 3);

			_objData.uvBuffer=this._context.createVertexBuffer(uu.length / 2, 2);
			_objData.uvBuffer.uploadFromVector(Vector.<Number>(uu), 0, uu.length / 2);

			_objData.indexBuffer=this._context.createIndexBuffer(ii.length);
			_objData.indexBuffer.uploadFromVector(Vector.<uint>(ii), 0, ii.length);
		}

		override public function set url(value:String):void
		{
			_url=value;
			var loaderinfo:LoadInfo=new LoadInfo(Scene_data.fileRoot + "build/obj/" + _url + ".obj", LoadInfo.XML, onObjLoad, false);
			LoadManager.getInstance().addSingleLoad(loaderinfo);
		}

		override protected function onObjLoad(str:String):void
		{
			_objData=AnalysisServer.getInstance().analysisObj(str);
			TextureManager.getInstance().addTexture(Scene_data.fileRoot + "build/texture/" + _objData.mtl + ".jpg", addTexture, null);
		}

        private var haveIt:Boolean=false;
		
		public function setGlassInfoBitmapdata(_bmp:BitmapData):void
		{
			if (!_stateBuffer ||timeToken||haveIt)
			{
				setTimeout(makeShadeTime,5*1000)
				function makeShadeTime():void
				{
					setGlassInfoBitmapdata(_bmp)
				}
				return;
			}
			haveIt=true;
			//通过地面绘画的艹图，生存真实的
			var vv:Vector.<Number>=new Vector.<Number>;
			var uu:Vector.<Number>=new Vector.<Number>;
			var ss:Vector.<Number>=new Vector.<Number>; //要确定每组成几个数据;暂为3跟顶点一样
			var ii:Vector.<uint>=new Vector.<uint>;
			var inum:int=_objData.vertices.length / 3;
			var xMove:Number=0;
			var zMove:Number=0;
			var sizeData:int=500;
			var angle:int=0;
			//
			var n:int=10;
			var w:int=int(_bmp.width / n)
			var modeID:Number=0;

			var timeToken:uint=setInterval(timeFun, 100)
			var lastTimer:int;
			var tatolTimes:int=n * n;
			var curTime:int=0
			function timeFun():void
			{
				trace("生成草皮:",modeID)
				lastTimer=getTimer();
				while (true)
				{
					var xpostion:int=int(curTime % n);
					var ypostion:int=int(curTime / n);
					//**********************************//
					var xp:int=int(xpostion * w)
					var yp:int=int(ypostion * w)
					var cp:uint=_bmp.getPixel(xp + (w / 2), yp + (w / 2));
					if (cp != 16777215) //不为空白，就显示有艹
					{
						vv=vv.concat(_objData.vertices);
						uu=uu.concat(_objData.uvs);
						xMove=xp+Math.random()*4;
						zMove=yp+Math.random()*4;
						for (var k:int=0; k < inum; k++)
						{
							ss.push(xMove*1.5+25, 0, zMove*1.5+25); // 偏移
						}
						for (var v:int=0; v < _objData.indexs.length; v++)
						{
							ii.push(_objData.indexs[v] + inum * modeID);
						}
						modeID++;
					}
					//**********************//
					curTime=curTime + 1;
					if (curTime >= tatolTimes)  //全部算过之后
					{
						clearInterval(timeToken)
						pushBuffData(vv, ss, uu, ii);
						break;
					}
					if ((getTimer() - lastTimer) > 50) //当时间超出设定的范围时设定这个进程不要超过50毫秒
					{
						break;
					}
				}
			}

		}

		private function cpcpCOPYcopyCOPY(_bmp:BitmapData):void
		{
			if (!_stateBuffer)
			{
				return;
			}
			//通过地面绘画的艹图，生存真实的
			var vv:Vector.<Number>=new Vector.<Number>;
			var uu:Vector.<Number>=new Vector.<Number>;
			var ss:Vector.<Number>=new Vector.<Number>; //要确定每组成几个数据;暂为3跟顶点一样
			var ii:Vector.<uint>=new Vector.<uint>;
			var inum:int=_objData.vertices.length / 3;
			var xMove:Number=0;
			var zMove:Number=0;
			var sizeData:int=500;
			var angle:int=0;
			//
			var n:int=50;
			var w:int=int(_bmp.width / n)
			var modeID:Number=0;


			for (var i:int=0; i < n; i++)
			{
				for (var j:int=0; j < n; j++)
				{
					var xp:int=int(i * w)
					var yp:int=int(j * w)
					var cp:uint=_bmp.getPixel(xp + (w / 2), yp + (w / 2));
					if (cp != 16777215)
					{
						vv=vv.concat(_objData.vertices);
						uu=uu.concat(_objData.uvs);
						xMove=xp - (_bmp.width / 2);
						zMove=yp;
						for (var k:int=0; k < inum; k++)
						{
							ss.push(xMove, 0, zMove); // 偏移
						}
						for (var v:int=0; v < _objData.indexs.length; v++)
						{
							ii.push(_objData.indexs[v] + inum * modeID);
						}
						modeID++;
					}
				}
			}
			pushBuffData(vv, ss, uu, ii);

		}

	}
}
